package com.goingup.domain;

import java.util.ArrayList;
import java.util.List;

import com.goingup.domain.Vector.Direction;


/**
 * A passenger travelling on an elevator.  Keeps track of its trip details.
 * @author pcresswell
 *
 */
public class Passenger {

	
	long startTime;
	long stopTime;
	private Floor floor;
	
	private Elevator elevator;
	private List<PassengerTrip> trips = new ArrayList<PassengerTrip>();
	private PassengerTrip currentTrip;
	
	public Passenger()
	{
		createNewTrip();
	}
	private void createNewTrip() {
		currentTrip = new PassengerTrip();
		currentTrip.setPassenger(this);
		trips.add(currentTrip);
	}
	/**
	 * Passenger has a desire to go to the given floor
	 * @param i
	 */
	public void setDestination(Floor destinationFloor) {
		currentTrip.setDestinationFloor(destinationFloor);
	}
	
	public List<PassengerTrip> getTripDetails()
	{
		return this.trips;
	}
	
	/**
	 * Board the given elevator.
	 * @param elevator
	 * @throws MaximumCapacityExceededException
	 */
	public void board(Elevator elevator) throws MaximumCapacityExceededException {
		System.out.print("Boarding elevator\n");
		elevator.addPassenger(this);
		setFloor(null);
		this.elevator = elevator;
		elevator.requestDropOffOn(currentTrip.getDestinationFloor());
	}
	/**
	 * Disembark from the elevator
	 */
	public void disembark()
	{
		elevator.remove(this);
		elevator.getCurrentFloor().addPassenger(this);
		setFloor(elevator.getCurrentFloor());
		currentTrip.stopClock();
		createNewTrip(); //each time we get off an elevator, create a new trip
		System.out.print("Disembarking elevator\n");
	}
	
	public boolean isDestinedFor(Floor floor) {
		if (floor==null)
			return false;
		if (currentTrip.getDestinationFloor()==null)
			return false;
		else
			return (currentTrip.getDestinationFloor().equals(floor));
	}
	
	public void setFloor(Floor floor) {
		this.floor = floor;
	}
	public Floor getCurrentFloor() {
		return floor;
	}
	public void requestElevator() {
		System.out.print("Requesting Elevator\n");
		currentTrip.setStartingFloor(getCurrentFloor());
		currentTrip.startClock();
		getCurrentFloor().requestElevator(getDirection());
	}
	private Direction getDirection()
	{
		if (this.currentTrip.getDestinationFloor().isAbove(this.floor))
			return Direction.UP;
		else
			return Direction.DOWN;
	}
	public boolean isOnFloor(Floor floor) {
		if (floor==null)
			return false;
		if (this.getCurrentFloor()==null)
			return false;
		else
			return getCurrentFloor().equals(floor);
	}
	public boolean hasDestination() {
		return (currentTrip.getDestinationFloor()!=null);
	}


	

}
